<?php

class User_Controller_Browse_Edit_Cover extends Nuo_Controller
{
	function execute($request)
	{
		$form = Nuo::getObject('user/photo/cover', 'form');

		$form -> setTitle('Change Profile Avatar');

		$mainResourceId = $request -> getParam('resource-item');

		if ($mainResourceId)
		{
			$mainResource = Nuo::getItem($mainResourceId);
		}

		if (!$mainResource)
		{
			$mainResource = Nuo::getUser();
		}

		Nuo::setVar('mainResource', $mainResource);

		if ($request -> isPost() && $form -> isValid($_POST))
		{
			$uploads = Nuo_Filesystem::instance() -> getUploads('photo');

			if ($uploads)
			{

				$file = Nuo::getApi('core/image')->process($uploads, array('n' => array(
						'width' => 687,
						'height' => 315,
						'quality' => 100
					)));

				if ($file)
				{
					$api = Nuo::getApi('photo');
					$api -> setCover($mainResource, $file);
				}

				$uri = $mainResource -> getUrl();

				Nuo::redirect($uri);

			}
		}
		return  $form;
	}

}
